[% setvar title use strict 'objects': a new pragma for using Java-like objects in Perl %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>use strict 'objects': a new pragma for using Java-like objects in Perl</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Mantainer:    Marco Marongiu &lt;<a href='mailto:bronto@crs4.it'>bronto@crs4.it</a>&gt;
  Date: 28 Sep 2000
  Mailing List: <a href='mailto:perl6-language-objects@perl.org'>perl6-language-objects@perl.org</a>
  Number: 336
  Version: 1
  Status: Developing
  Co-mantainer: Stefano Sanna  &lt;<a href='mailto:gerda@crs4.it'>gerda@crs4.it</a>&gt;</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>As noted in many other RFCs, many people in the Perl community are
asking for a better implementation of objects in Perl. D.Conway in
his RFC 188 gave us a good design for implementing private objects
and attributes in Perl. Our humble effort is to go one step forward,
slightly changing the RFC 188 requests and proposing two more unary
operators: protected and static.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>This RFC comes out from a discussion between the authors: a Perl
programmer and a Java programmer. It came out that the sleeping
hours we lost were worth an RFC, where together we proposed a
modification of objects implementation in Perl.</p>
<p>We choose to ask for a pragma, since we didn't like the idea of
Perl programmers forced to use the new operators that we are to propose.
As people is not forced to predeclare subroutines, variables and so on,
people will not be forced to declare an attribute as private or a
method as static. Perlers' laziness must be preserved!</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>We propose to define a new schema to the <code>strict</code> pragma: <code>use
strict 'objects'</code>. Use-ing strict objects imports in the calling
package (or in all packages if declared inside main) three unary
operators: <code>private</code>, <code>protected</code> and <code>static</code>, that behave as
stated below.</p>
<a name='protected'></a><h2>protected</h2>
<p>Just take Conway's RFC 188 and do a s/private/protected/g :-)</p>
<a name='private'></a><h2>private</h2>
<p>Like Conway's private operator defined in RFC 188, but accessing an
hash key or a method that has been marked as private from outside the
defining package should result in a fatal error.</p>
<pre>        use strict 'objects' ;

        package Base;
        
        sub new {
                my ($class, @data) = @_;
                bless private { data =&gt; [@data] }, $class;
        }
        
        
        package SortableBase;
        use base 'Base';
                
        sub sorted {
                my ($self) = @_;
                print sort @{ $self-&gt;{Base::data} }; # should die now!
        }</pre>
<a name='static'></a><h2>static</h2>
<p>In our humble opinion, static doesn't make sense for
attributes... erm... hashes or hash slices in perl. Anyway, we think
it still makes sense to have static methods.</p>
<p>Static methods should be considered as public methods (unless
otherwise specified via the private or protected operator)  that can be
invoked on bare classes directly, without needing an object
instance. Methods not marked as such should return a fatal error if called
directly on the class. For example:</p>
<pre>
    package NoStatic ;

    sub new { bless { foo =&gt; 'bar' } }
    sub wen { print &quot;Windows' evil nature&quot; }


    package Static ;

    use strict 'objects' ;

    static sub new { bless { foo =&gt; 'bar' } }
    sub wen { print &quot;Windows' evil nature&quot; }


    package main ;

    my $nsobj = NoStatic-&gt;new() ; # ok
    $nsobj-&gt;wen() ; # ok
    NoStatic-&gt;wen() ; # ok
    
    my $stobj = Static-&gt;new() ; # ok
    $stobj-&gt;wen() ; # ok
    Static-&gt;wen() ; # dies (not declared as static)</pre>
<p>Subclasses should be allowed to overload a static
method only with a prototyped one. Other attempts to redefine a static
method should result in a fatal error.</p>
<p>A static method can also be declared as protected or private, prefixing
with the desired operator:</p>
<pre>
    use strict 'objects' ;

    package MyClass ;

    private static sub _do_cleanup {
        # do something
    }

    sub cleaner {
        # do something
        MyClass-&gt;_do_cleanup ;
        # do something more
    }</pre>
<p>Static methods would be needed when we want to have access to an
object oriented interface to functions. For example, it would be nice
if you could do something like:</p>
<pre>
    use strict 'objects' ;

    use POSIX @import_something ;

    my $obj = new MyClass @params ;
    # do something really-OO-style here
    # and you can stand OO here, too
    print POSIX-&gt;floor(3.1415926) ;</pre>
<p>instead of</p>
<pre>
    use POSIX @import_something ;

    my $obj = new MyClass @params ;
    # do something really-OO-style here, then
    print floor(3.1415926) ;
    # Not so OO, indeed 
    </pre>
<p>but this is just syntactic sugar :-)</p>
<a name='BUGS'></a><h1>BUGS</h1>
<p>Our english is not very good. In case of errors we apologize.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 188: Objects : Private keys and methods</p>
</div>
